﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Data
Imports System.Windows.Forms
Imports DevComponents.DotNetBar.Controls
Imports DevComponents.Editors.DateTimeAdv
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.DTO
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.General
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.BUSS

Namespace DoAnQuanLyBanHang.DAO
    Public Class MatHangCtrl
        Private m_MatHangData As New MatHangData()
        Private m_MatHangInfo As New MatHangInfo()
        Public Sub HienThiComboBox(cbx As ComboBox)
            Dim m_DT As DataTable = m_MatHangData.LayMatHang()
            cbx.DataSource = m_DT
            cbx.DisplayMember = "TenMH"

            cbx.ValueMember = "MaMH"


        End Sub

        Public Sub HienThiComboBox1(cbx As ComboBox)
            Dim m_DT As DataTable = m_MatHangData.LayMatHang()
            cbx.DataSource = m_DT
            cbx.DisplayMember = "MaPhieu"

            cbx.ValueMember = "MaPhieu"
        End Sub
        Public Shared Function LayDsMatHang() As IList(Of MatHangInfo)
            Dim ds As IList(Of MatHangInfo) = New List(Of MatHangInfo)()

            Dim data As New MatHangData()
            Dim tbl As DataTable = data.LayDSMatHang()

            Dim MatHang As IList(Of MatHangInfo) = New List(Of MatHangInfo)()

            For Each row As DataRow In tbl.Rows
                Dim MH As New MatHangInfo()

                Dim m_NCC As New NhaCungCapInfo()
                m_NCC.TenNCC = Convert.ToString(row("TenNCC"))
                Dim m_LoaiTien As New LoaiTienInfo()
                m_LoaiTien.LoaiTien = Convert.ToString(row("LoaiTien"))
                Dim m_LoaiGD As New LoaiGiaoDichInfo()
                m_LoaiGD.TenGiaoDich = Convert.ToString(row("TenLoaiGiaoDich"))

                MH.MaPhieu = Convert.ToString(row("MaPhieu"))
                MH.MaMH = Convert.ToString(row("MaMH"))
                MH.TenMH = Convert.ToString(row("TenMH"))
                MH.SoLuong = Convert.ToString(row("SoLuong"))
                MH.DonGia = Convert.ToString(row("DonGia"))
                MH.GiaBan = Convert.ToString(row("GiaBan"))
                MH.NgayNhap = Convert.ToDateTime(row("NgayNhap"))
                MH.MaNCC = m_NCC
                MH.MaLoaiGiaoDich = m_LoaiGD
                MH.MaLoaiTien = m_LoaiTien
                MatHang.Add(MH)
            Next
            Return MatHang
        End Function

        Public Shared Function LayDsMatHangTheoMa(MaPhieu As [String]) As IList(Of MatHangInfo)
            Dim ds As IList(Of MatHangInfo) = New List(Of MatHangInfo)()

            Dim data As New MatHangData()
            Dim tbl As DataTable = data.LayDSMatHangTheoMa(MaPhieu)

            Dim MatHang As IList(Of MatHangInfo) = New List(Of MatHangInfo)()

            For Each row As DataRow In tbl.Rows
                Dim MH As New MatHangInfo()

                Dim m_NCC As New NhaCungCapInfo()
                m_NCC.TenNCC = Convert.ToString(row("TenNCC"))
                m_NCC.MaNCC = Convert.ToString(row("MaNCC"))
                Dim m_LoaiTien As New LoaiTienInfo()
                m_LoaiTien.LoaiTien = Convert.ToString(row("LoaiTien"))
                Dim m_LoaiGD As New LoaiGiaoDichInfo()
                m_LoaiGD.TenGiaoDich = Convert.ToString(row("TenLoaiGiaoDich"))

                MH.MaPhieu = Convert.ToString(row("MaPhieu"))
                MH.MaMH = Convert.ToString(row("MaMH"))
                MH.TenMH = Convert.ToString(row("TenMH"))
                MH.SoLuong = Convert.ToString(row("SoLuong"))
                MH.DonGia = Convert.ToString(row("DonGia"))
                MH.GiaBan = Convert.ToString(row("GiaBan"))
                MH.NgayNhap = Convert.ToDateTime(row("NgayNhap"))
                MH.MaNCC = m_NCC
                MH.MaLoaiGiaoDich = m_LoaiGD
                MH.MaLoaiTien = m_LoaiTien
                MatHang.Add(MH)
            Next
            Return MatHang
        End Function

#Region "Hien thi ComboBox trong DataGridView"
        Public Sub HienThiDataGridViewComboBoxColumn(cmbColumn As DataGridViewComboBoxColumn)
            cmbColumn.DataSource = m_MatHangData.LayMatHang()
            cmbColumn.DisplayMember = "TenMH"
            cmbColumn.ValueMember = "MaMH"
            cmbColumn.DataPropertyName = "MaMH"
            cmbColumn.HeaderText = "Tên mặt hàng"
        End Sub
#End Region

#Region "hien thi"
        Public Sub HienThi(Dgv As DataGridView, bvn As BindingNavigator)
            Dim bs As New BindingSource()

            bs.DataSource = m_MatHangData.LayMatHang()
            bvn.BindingSource = bs
            Dgv.DataSource = bs
        End Sub

        Public Sub HienThi(dGV As DataGridViewX, bN As BindingNavigator, txtMaPhieu As TextBoxX, txtMaMH As TextBoxX, txtTenMH As TextBoxX, dtNgayNhap As DateTimeInput, _
         txtSoLuong As TextBoxX, txtDonGia As TextBoxX, txtGiaBan As TextBoxX, txtMaNCC As TextBoxX, cmbTenNCC As ComboBoxEx, cmbLoaiGiaoDich As ComboBoxEx, _
         cmbLoaiTienGui As ComboBoxEx)
            Dim bS As New BindingSource()
            bS.DataSource = m_MatHangData.LayMatHang()

            txtMaPhieu.DataBindings.Clear()
            txtMaPhieu.DataBindings.Add("Text", bS, "MaPhieu")

            txtMaMH.DataBindings.Clear()
            txtMaMH.DataBindings.Add("Text", bS, "MaMH")

            txtTenMH.DataBindings.Clear()
            txtTenMH.DataBindings.Add("Text", bS, "TenMH")

            dtNgayNhap.DataBindings.Clear()
            dtNgayNhap.DataBindings.Add("Value", bS, "NgayNhap")

            txtSoLuong.DataBindings.Clear()
            txtSoLuong.DataBindings.Add("Text", bS, "SoLuong")

            txtDonGia.DataBindings.Clear()
            txtDonGia.DataBindings.Add("Text", bS, "DonGia")


            txtGiaBan.DataBindings.Clear()
            txtGiaBan.DataBindings.Add("Text", bS, "GiaBan")

            txtMaNCC.DataBindings.Clear()
            txtMaNCC.DataBindings.Add("Text", bS, "MaNCC")

            cmbLoaiGiaoDich.DataBindings.Clear()
            cmbLoaiGiaoDich.DataBindings.Add("SelectedValue", bS, "MaLoaiGiaoDich")



            cmbTenNCC.DataBindings.Clear()
            cmbTenNCC.DataBindings.Add("SelectedValue", bS, "MaNCC")

            cmbLoaiTienGui.DataBindings.Clear()
            cmbLoaiTienGui.DataBindings.Add("SelectedValue", bS, "MaLoaiTien")

            bN.BindingSource = bS
            dGV.DataSource = bS
        End Sub
#End Region

#Region "Them moi"
        Public Function ThemDongMoi() As DataRow
            Return m_MatHangData.ThemDongMoi()
        End Function
        Public Sub ThemMoi(m_Row As DataRow)
            m_MatHangData.ThemMatHang(m_Row)
        End Sub
#End Region

#Region "Luu"
        Public Function LuuMatHang() As Boolean
            Return m_MatHangData.LuuMatHang()
        End Function

        Public Sub LuuMatHang(maphieu As [String], mamh As [String], tenmh As [String], soluong As Single, dongia As Single, giaban As Single, _
         mancc As [String], ngaynhap As DateTime, maloaitien As [String], maloaigd As [String])
            m_MatHangData.LuuMatHang(maphieu, mamh, tenmh, soluong, dongia, giaban, _
             mancc, ngaynhap, maloaitien, maloaigd)
        End Sub
#End Region
#Region "Sửa"
        Public Sub SuaMatHang(maphieu As [String], mamh As [String], tenmh As [String], soluong As Single, dongia As Single, giaban As Single, _
         mancc As [String], ngaynhap As DateTime, maloaitien As [String], maloaigd As [String])
            m_MatHangData.SuaMatHang(maphieu, mamh, tenmh, soluong, dongia, giaban, _
             mancc, ngaynhap, maloaitien, maloaigd)
        End Sub
#End Region

#Region "Xóa"
        Public Sub XoaMatHang(maphieu As [String], mamh As [String])
            m_MatHangData.XoaMatHang(maphieu, mamh)
        End Sub
#End Region
#Region "tim kiem"
        Public Sub TimKiemTheoMa(m_MaMH As [String])
            m_MatHangData.TimKiemTheoMa(m_MaMH)
        End Sub

        Public Sub TiemKiemTheoTen(m_TenMH As [String])
            m_MatHangData.TimKiemTheoTen(m_TenMH)
        End Sub
#End Region
    End Class
End Namespace
